﻿using System;
using System.Xml;
using System.Xml.XPath;
using FeedsLibrary.Feeds.Abstract;

namespace FeedsLibrary.Feeds.RDF
{
    public class RDFChannel : AbstractChannel
    {
        public RDFChannel()
        {
            Days = 100;
            TagsOnly = false;
        }

        public RDFChannel(Uri url, Uri iconUrl, DateTime currentDay, int days, bool tagsOnly)
        {
            CurrentDay = currentDay;
            Days = days;
            TagsOnly = tagsOnly;
            LoadDocumentFromUrl(url, iconUrl);
            LoadBody(false);
        }

        protected override void LoadBody(bool update)
        {
            XmlNode n = Document;

            if (n.InnerText != null)
            {
                n = FindElement(n, "rdf:RDF");

                n = FindElement(n, "channel");
                LoadHeader(n);

                n = Document;
                n = FindElement(n, "rdf:RDF");

                LoadItems(n, "item", update);
            }
            Document = null;
        }

        public override IItem GetNewItem(string domain, IXPathNavigable nodeItem, DateTime lastBuildDate)
        {
            return new RDFItem(domain, nodeItem);
        }
    }
}